using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsPhoneGame3
{
    public class Computer
    {
        int[,] map = new int[3, 3];
        private Vector2 vitriDanh;
        int x, y;
        Random ran = new Random(); //Ham random vi tri
        public Computer()
        {
            vitriDanh = new Vector2(2, 2);//khoi tao gia tri cho vitridanh
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    map[i, j] = 0;//khoi tao map trong
                }
            }
        }
        public void setmap(int[,] a)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    map[i, j] = a[i, j]; //cap nhat map hien tai
                }
            }
        }
        public Vector2 getViTri()
        {
            return vitriDanh;
        }
        public void setVitri(int x, int y)
        {
            vitriDanh.X = x;
            vitriDanh.Y = y;
        }
        public void Play()
        {
            vitriDanh.X = 3;
            //bo kiem tra thang.
            ktBossThang1();
            ktBossThang2();
            ktBossThang3();
            ktBossThang4();
            ktBossThang5();
            ktBossThang6();
            ktBossThang7();
            ktBossThang8();
            if (vitriDanh.X == 3)
            {
                //kiem tra nguoi choi co the thang khong?
                ktPlayerThang1();
                ktPlayerThang2();
                ktPlayerThang3();
                ktPlayerThang4();
                ktPlayerThang5();
                ktPlayerThang6();
                ktPlayerThang7();
                ktPlayerThang8();
            }
            if (vitriDanh.X == 3)
            {
                do
                {
                    x = ran.Next() % 3;
                    y = ran.Next() % 3;
                } while (map[x, y] != 0);
                setVitri(x, y);
            }
        }
        public void ktPlayerThang1()
        {
            if (map[0, 0] == 1 && map[1, 0] == 1 && map[2, 0] == 0) setVitri(2, 0);
            if (map[0, 0] == 1 && map[1, 0] == 0 && map[2, 0] == 1) setVitri(1, 0);
            if (map[0, 0] == 0 && map[1, 0] == 1 && map[2, 0] == 1) setVitri(0, 0);
        }
        public void ktPlayerThang2()
        {
            if (map[0, 1] == 1 && map[1, 1] == 1 && map[2, 1] == 0) setVitri(2, 1);
            if (map[0, 1] == 1 && map[1, 1] == 0 && map[2, 1] == 1) setVitri(1, 1);
            if (map[0, 1] == 0 && map[1, 1] == 1 && map[2, 1] == 1) setVitri(0, 1);
        }
        public void ktPlayerThang3()
        {
            if (map[0, 2] == 1 && map[1, 2] == 1 && map[2, 2] == 0) setVitri(2, 2);
            if (map[0, 2] == 1 && map[1, 2] == 0 && map[2, 2] == 1) setVitri(1, 2);
            if (map[0, 2] == 0 && map[1, 2] == 1 && map[2, 2] == 1) setVitri(0, 2);
        }
        public void ktPlayerThang4()
        {
            if (map[0, 0] == 1 && map[0, 1] == 1 && map[0, 2] == 0) setVitri(0, 2);
            if (map[0, 0] == 1 && map[0, 1] == 0 && map[0, 2] == 1) setVitri(0, 1);
            if (map[0, 0] == 0 && map[0, 1] == 1 && map[0, 2] == 1) setVitri(0, 0);
        }
        public void ktPlayerThang5()
        {
            if (map[1, 0] == 1 && map[1, 1] == 1 && map[1, 2] == 0) setVitri(1, 2);
            if (map[1, 0] == 1 && map[1, 1] == 0 && map[1, 2] == 1) setVitri(1, 1);
            if (map[1, 0] == 0 && map[1, 1] == 1 && map[1, 2] == 1) setVitri(1, 0);
        }
        public void ktPlayerThang6()
        {
            if (map[2, 0] == 1 && map[2, 1] == 1 && map[2, 2] == 0) setVitri(2, 2);
            if (map[2, 0] == 1 && map[2, 1] == 0 && map[2, 2] == 1) setVitri(2, 1);
            if (map[2, 0] == 0 && map[2, 1] == 1 && map[2, 2] == 1) setVitri(2, 0);
        }
        public void ktPlayerThang7()
        {
            if (map[0, 0] == 1 && map[1, 1] == 1 && map[2, 2] == 0) setVitri(2, 2);
            if (map[0, 0] == 1 && map[1, 1] == 0 && map[2, 2] == 1) setVitri(1, 1);
            if (map[0, 0] == 0 && map[1, 1] == 1 && map[2, 2] == 1) setVitri(0, 0);
        }
        public void ktPlayerThang8()
        {
            if (map[2, 0] == 1 && map[1, 1] == 1 && map[0, 2] == 0) setVitri(0, 2);
            if (map[2, 0] == 1 && map[1, 1] == 0 && map[0, 2] == 1) setVitri(1, 1);
            if (map[2, 0] == 0 && map[1, 1] == 1 && map[0, 2] == 1) setVitri(2, 0);
        }
        public void ktBossThang1()
        {
            if (map[0, 0] == 2 && map[1, 0] == 2 && map[2, 0] == 0) setVitri(2, 0);
            if (map[0, 0] == 2 && map[1, 0] == 0 && map[2, 0] == 2) setVitri(1, 0);
            if (map[0, 0] == 0 && map[1, 0] == 2 && map[2, 0] == 2) setVitri(0, 0);
        }
        public void ktBossThang2()
        {
            if (map[0, 1] == 2 && map[1, 1] == 2 && map[2, 1] == 0) setVitri(2, 1);
            if (map[0, 1] == 2 && map[1, 1] == 0 && map[2, 1] == 2) setVitri(1, 1);
            if (map[0, 1] == 0 && map[1, 1] == 2 && map[2, 1] == 2) setVitri(0, 1);
        }
        public void ktBossThang3()
        {
            if (map[0, 2] == 2 && map[1, 2] == 2 && map[2, 2] == 0) setVitri(2, 2);
            if (map[0, 2] == 2 && map[1, 2] == 0 && map[2, 2] == 2) setVitri(1, 2);
            if (map[0, 2] == 0 && map[1, 2] == 2 && map[2, 2] == 2) setVitri(0, 2);
        }
        public void ktBossThang4()
        {
            if (map[0, 0] == 2 && map[0, 1] == 2 && map[0, 2] == 0) setVitri(0, 2);
            if (map[0, 0] == 2 && map[0, 1] == 0 && map[0, 2] == 2) setVitri(0, 1);
            if (map[0, 0] == 0 && map[0, 1] == 2 && map[0, 2] == 2) setVitri(0, 0);
        }
        public void ktBossThang5()
        {
            if (map[1, 0] == 2 && map[1, 1] == 2 && map[1, 2] == 0) setVitri(1, 2);
            if (map[1, 0] == 2 && map[1, 1] == 0 && map[1, 2] == 2) setVitri(1, 1);
            if (map[1, 0] == 0 && map[1, 1] == 2 && map[1, 2] == 2) setVitri(1, 0);
        }
        public void ktBossThang6()
        {
            if (map[2, 0] == 2 && map[2, 1] == 2 && map[2, 2] == 0) setVitri(2, 2);
            if (map[2, 0] == 2 && map[2, 1] == 0 && map[2, 2] == 2) setVitri(2, 1);
            if (map[2, 0] == 0 && map[2, 1] == 2 && map[2, 2] == 2) setVitri(2, 0);
        }
        public void ktBossThang7()
        {
            if (map[0, 0] == 2 && map[1, 1] == 2 && map[2, 2] == 0) setVitri(2, 2);
            if (map[0, 0] == 2 && map[1, 1] == 0 && map[2, 2] == 2) setVitri(1, 1);
            if (map[0, 0] == 0 && map[1, 1] == 2 && map[2, 2] == 2) setVitri(0, 0);
        }
        public void ktBossThang8()
        {
            if (map[2, 0] == 2 && map[1, 1] == 2 && map[0, 2] == 0) setVitri(0, 2);
            if (map[2, 0] == 2 && map[1, 1] == 0 && map[0, 2] == 2) setVitri(1, 1);
            if (map[2, 0] == 0 && map[1, 1] == 2 && map[0, 2] == 2) setVitri(2, 0);
        }
    }
}
